package com.example.dream.learnandroid.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;
import android.util.Log;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Permission;

import static android.content.Context.TELEPHONY_SERVICE;

@SuppressWarnings({"SpellCheckingInspection", "unused"})
public class UniqueID {
    private static String TAG = "UniqueID";
    /**
     * 获取手机IMEI号 需检查是否非空
     * @param context
     * @return IMEI String
     */
    public static String getIMEI(Context context) {
        try {
            TelephonyManager TelephonyMgr = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
                return TelephonyMgr.getDeviceId();
            } else {
                return "";
            }
        }catch (Exception e) {
            return "";
        }
    }

    public static String getDeviceName(){
        return Build.MODEL;
    }
    public static String getMacAddress(Context context) {
        try {
            if(ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_WIFI_STATE) == PackageManager.PERMISSION_GRANTED){
                WifiManager wifi = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
                WifiInfo info = wifi.getConnectionInfo();
                return info.getMacAddress();
            }else{
                return "";
            }
        }catch (Exception e) {
            return "";
        }
    }

    public static String getAndroidID(Context context){
        return Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);
    }

    public static String getSerialNum(){
         return Build.SERIAL;
    }

    public static String getUniqueId(Context context){
        String androidID = getAndroidID(context);
        Log.d(TAG, "androidID: " + androidID);

        String imei = getIMEI(context);
        Log.d(TAG, "imei: " + imei);

        String deviceName = getDeviceName();
        Log.d(TAG, "deviceName: " + deviceName);

        String macAddress = getMacAddress(context);
        Log.d(TAG, "macAddress: " + macAddress);

        String serialNum = getSerialNum();
        Log.d(TAG, "serialNum: " + serialNum);

        String idRawStr = androidID + imei + deviceName + macAddress + serialNum;
        Log.d(TAG, "idRawStr: " + idRawStr);

        try {
            return toMD5(idRawStr);
        }catch (NoSuchAlgorithmException e){
            e.printStackTrace();
            return idRawStr;
        }

    }
    private static String toMD5(String text)  throws NoSuchAlgorithmException {
        //获取摘要器 MessageDigest
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        //通过摘要器对字符串的二进制字节数组进行hash计算
        byte[] digest = messageDigest.digest(text.getBytes());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < digest.length; i++)
        {
            //循环每个字符 将计算结果转化为正整数;
            int digestInt = digest[i] & 0xff;
            //将10进制转化为较短的16进制
            String hexString = Integer.toHexString(digestInt);
            //转化结果如果是个位数会省略0,因此判断并补0
            if (hexString.length() < 2)
            {
                sb.append(0);
            }
            //将循环结果添加到缓冲区
            sb.append(hexString);
        }
        //返回整个结果
        return sb.toString();
    }
}
